<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        header('Content-Type: application/json; charset=utf-8');
		$data = M('User');   //table_name
        if(session('auth'))
        {
            $this->redirect('index/task',0);
        }
		if(IS_POST && __hash__ <> ""){
			$pwd = $_POST['tk_user_pass'] = md5(crypt($_POST['tk_user_pass'],substr($_POST['tk_user_pass'],0,2)));
		    $usr = $_POST['tk_user_login'];
			$condition['tk_user_login'] = $usr;
			$return = $data->where($condition)->find();
					if($return['tk_user_pass'] == $pwd)
						{
                            session('auth', true);
							$this->success('欢迎登陆！','index/index/task.html');
						}else{
							$this->error('非法操作');
						}
		}else{
			$this->display();
		}
		

    }
	
	public function sign(){
        header('Content-Type: application/json; charset=utf-8');
        if(session('auth'))
        {
            $this->error('您已经登陆！','task.html');
        }
		if(IS_POST){
			$_POST['tk_user_pass'] = md5(crypt($_POST['tk_user_pass'],substr($_POST['tk_user_pass'],0,2)));
			unset($_POST['__hash__']);
			//dump($_POST);
			$data = M('User');   //table_name
			$insertID = $data->add($_POST);
			//echo $data->getlastsql();
			//$insertID = $data->update($_POST, 'id=1');delete select(); where()->count();
			//$return = $data->where('uid = 1')->find();
			//print_r($return['tk_user_login']);
			$this->success("注册成功！",'index/index');
		}else{
			$this->display();
		}
	}
	
	public function task(){
        header('Content-Type: application/json; charset=utf-8');
        if(session('auth')){
            if(IS_POST){
                $data = M('Project');   //table_name
                // 写入操作员和状态
                $_POST['project_to_user'] = 1;
                $_POST['project_status'] = 2;
                unset($_POST['__hash__']);
                $post = array_merge($_POST, array(
                    'project_to_code'   => "",
                    'project_type' => "",
                    'project_from' => "",
                    'project_from_user' => "",
                    'project_from_contact' => "",
                    'project_to_dept' => "",
                    'project_remark' => "",
                    'project_lastupdate' => time()
                ));
                $insertID = $data->add($_POST);
                /* $_POST['project_name'] = mb_convert_encoding($_POST['project_name'],'GBK','UTF-8');
                 $_POST['project_text'] = mb_convert_encoding($_POST['project_text'],'GBK','UTF-8');*/
                //dump(convert_encoding($post, "GBK", "UTF-8"));
                //print_r($post);
                $this->success("任务添加成功！");
            }else{
                $this->display();
            }
        }else{
            $this->error('请登录后再使用!','index.html');
        }

	}

	FUNCTION sdgsdg(){
		header('Content-Type: application/json; charset=utf-8');
		$dtPrj = M('Project');
		$resProject = $dtPrj->where()->select();
		foreach($resProject AS $i => $v){
			$dtTsk = M('Task');
			$resTask = $dtTsk->where('csa_project = '.$v['id'])->select();
			$Project[$i] = array_merge($resProject[$i], array('task' => $resTask));
		}
		print_r($Project);
	}


    public function view()
    {// 任务查阅
	    $dtPrj = M('Project');
	    $resProject = $dtPrj->where()->select();
	    foreach($resProject AS $i => $v){
		    $dtTask = M('Task');
		    $resTask = $dtTask->where('csa_project = '.$v['id'])->select();
		    /*foreach($resTask AS $k => $vU)
		    {*/
			    $dtUser = M('User');
			    $resUser = $dtUser->where('uid = '.$v['csa_to_user'])->select();
			    $dtTaskStat = M('Status');
			    $resTaskStat = $dtTaskStat->where('id = '.$v['csa_remark2'])->select();
			    $Project[$i] = array_merge($resProject[$i], array('task' => $resTask), array('user' => $resUser), array('taskstat' => $resTaskStat));
		    /*}*/
	    }

	    $this->assign('Project',$Project);
	    /*$dtPrj = M('Project');
	    $resProject = $dtPrj->where()->select();
	    $this->assign('views',$resProject);                    // 项目数据表，以及SQL
	    $dtTsk = M('Task');
	    foreach($resProject as $key=>$val)
	    {
		    $sqlTask['csa_project'] = $resProject[$val['id']]['csa_remark2'];
		    $resTask = $dtTsk->where($sqlTask)->select();
		    $this->assign('TaskStat',$resTask[$val['id']]['csa_remark2']);  // 任务派发表，SQL
	    }*/

	// 登陆后继续读取数据到前台。
        if(session('auth'))
        {// 若已登录
	        $this->display();   // 前台输出
        }else{
            $this->error('请您先登陆！','index.html');
        }
    }
}

